package com.csc.yunting.core.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csc.yunting.base.result.ResponseVo;
import com.csc.yunting.core.pojo.entity.Artist;
import com.csc.yunting.core.pojo.vo.ArtistVO;
import com.csc.yunting.core.service.ArtistService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/admin/main/artist")
public class AdminArtistCotroller {
    @Resource
    ArtistService artistService;

    @PostMapping("/findAll")
    public ResponseVo findAll(@RequestBody ArtistVO artistVO) {
        IPage page = artistService.findList(artistVO);
//        return ResponseVo.ok().data("page", page);
        Page page1 = artistService.page(new Page(artistVO.getPage(), artistVO.getSize()));
        return ResponseVo.ok().data("page", page1);
    }

    @GetMapping("/findById/{id}")
    public ResponseVo findById(@PathVariable Long id) {
        LambdaQueryWrapper<Artist> wrapper = new LambdaQueryWrapper<>();
        Artist one = artistService.getOne(wrapper.eq(Artist::getId, id.longValue()));
        return ResponseVo.ok().data("data", one);
    }

    @PostMapping("/add")
    public ResponseVo addArtist(@RequestBody ArtistVO artistVO) {
        Artist artist = new Artist();
        BeanUtils.copyProperties(artistVO, artist);
        artistService.save(artist);
        return ResponseVo.ok();
    }

}
